﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.DirectoryServices;
using System.Configuration;
using System.Data;

namespace SAP
{
    public partial class ADLogin : System.Web.UI.Page
    {
        #region Active Directory Specific

        protected string JobTitle = "";
        protected string DirectManagerID = "";
        protected string ADDistingushedName = "";
        protected string Department = "";
        protected string Email = "";
        protected string LastPassword = "";

        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {

        }


        public bool ActiveDirectoryLogin()
        {
            
       
            bool smooth = true;

            DirectoryEntry CurADSystem = null;
            DirectorySearcher CurADSearcher = null;
            SearchResult CurSearchResult = null;

            string LDAP_Address = ConfigurationManager.AppSettings["LDAP"].ToString();
            // "LDAP://ABEOVN.COM/CN=Users,DC=ABEOVN,DC=COM";

            if (LDAP_Address.Length > 0)
            {
                try
                {
                    CurADSystem = new DirectoryEntry(LDAP_Address, TextBox_LoginID.Text, TextBox_Password.Text);
                    CurADSearcher = new DirectorySearcher(CurADSystem);
                    CurADSearcher.Filter = "samaccountname=" + TextBox_LoginID.Text;
                    CurADSearcher.SearchScope = SearchScope.Subtree;
                    CurSearchResult = CurADSearcher.FindOne();


                    if (CurSearchResult != null)
                    {
                        ResultPropertyCollection CurResultProperties = CurSearchResult.Properties;
                        foreach (string PropertyKey in CurResultProperties.PropertyNames)
                        {
                            switch (PropertyKey)
                            {
                                case "title":

                                    foreach (Object TmpCollection in CurResultProperties[PropertyKey])
                                    {
                                        this.JobTitle += TmpCollection.ToString();
                                    }

                                    break;

                                case "manager":

                                    foreach (Object TmpCollection in CurResultProperties[PropertyKey])
                                    {
                                        this.DirectManagerID += TmpCollection.ToString();
                                    }
                                    break;

                                case "distinguishedname":

                                    foreach (Object TmpCollection in CurResultProperties[PropertyKey])
                                    {
                                        this.ADDistingushedName += TmpCollection.ToString();
                                    }
                                    break;

                                case "mail":
                                    foreach (Object TmpCollection in CurResultProperties[PropertyKey])
                                    {
                                        this.Email += TmpCollection.ToString();
                                    }
                                    break;

                                case "PwdLastSet":
                                    foreach (Object TmpCollection in CurResultProperties[PropertyKey])
                                    {
                                        this.LastPassword += TmpCollection.ToString();
                                    }
                                    break;

                                case "department":
                                    foreach (Object TmpCollection in CurResultProperties[PropertyKey])
                                    {
                                        this.Department += TmpCollection.ToString();
                                    }
                                    break;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    smooth = false;
                    Label_Message.Text = ex.ToString();
                }
                finally
                {
                    // clean up
                    CurADSystem.Close();
                    CurADSystem.Dispose();

                    CurADSearcher.Dispose();
                    CurSearchResult = null;
                }
            }
            else
            {
                smooth = false;
            }



            return smooth;
        }
        protected void Button_Login_Click(object sender, EventArgs e)
        {
            

            if (ConfigurationManager.AppSettings["GhostMode"].ToString()=="1")
            {
                if (GetSessionInfo())
                    Response.Redirect("Homepage.aspx");
                else
                    Label_Message.Text = "User Name is not existed"; 
            }
            else
            {
                if (this.ActiveDirectoryLogin())
                {
                    if (GetSessionInfo())
                        Response.Redirect("Homepage.aspx");
                    else
                        Label_Message.Text = "User Name is not existed"; 
                }
            }

        }

        private bool GetSessionInfo()
        {
            LoginSession CurrentLoginSession = new LoginSession();
            DataTable dt = new DataTable();
            GetDefault df = new GetDefault();


            dt = df.GetLoginInfo(TextBox_LoginID.Text, 0).Tables[0];
            if (dt.Rows.Count > 0)
            {
                CurrentLoginSession.UserName = TextBox_LoginID.Text;
                CurrentLoginSession.UserEmpID = dt.Rows[0]["EmpID"].ToString();
                CurrentLoginSession.ManagerEmpID = dt.Rows[0]["managerID"].ToString();
                CurrentLoginSession.OutletCode = dt.Rows[0]["U_DefaultWhs"].ToString();
                CurrentLoginSession.ManagerEmail = dt.Rows[0]["ManagerEmail"].ToString();
                CurrentLoginSession.EmployeeEmail = dt.Rows[0]["EmployeeEmail"].ToString();
                CurrentLoginSession.DeptName = dt.Rows[0]["DepartmentName"].ToString();
                CurrentLoginSession.CompanyName = dt.Rows[0]["CompanyName"].ToString();
                CurrentLoginSession.HotelName = dt.Rows[0]["Hotel"].ToString();
                CurrentLoginSession.ManagerUserName = dt.Rows[0]["ManagerLoginID"].ToString();

                Session["LoginSession"] = CurrentLoginSession;
            }
            else
                return false;
            
            return true;
        }
    }
}
